One hundred people line up to board an airplane. Each has a boarding pass with assigned seat. However, the first person to board has lost his boarding pass and takes a random seat. After that, each person takes the assigned seat if it is unoccupied, and one of unoccupied seats at random otherwise. What is the probability that the last person to board gets to sit in his assigned seat?


In [1]:
from random import randint
def r(n):
    return randint(0, n - 1)
def f(n):
    if 1 == n:
        return True
    s = [True] * n
    s[r(n)] = False
    def g(m):
        if s[m]:
            s[m] = False
            return
        i = r(n - m)
        for j in range(n):
            if 0 == i and s[j]:
                s[j] = False
                return
            if s[j]:
                i -= 1
    for i in range(1, n - 1):
        g(i)
    return not s[n - 1]

print f(1)
samples = 100000
for i in range(1, 11):
    print sum([f(i) for _ in range(samples)]) / float(samples)


True
1.0
0.49897
0.49925
0.49925
0.50093
0.49809
0.50402
0.50137
0.49903
0.50172

In [2]:
from math import fsum
print sum([0.1] * 10)
print fsum([0.1] * 10)


1.0
1.0